/*
 *@Description: 根据起点中文网爬取 小说书名
 *@Author:  lupan
 *@Date: 2019-06-19 08:17:03
*/
const {BookMenu} = require('../../db/model/index')

/*
 *@Description: 采集小说菜单
 *@params: 
 *@Author:  lupan
 *@Date: 2019-06-19 10:53:33
*/
const gatMenu = async (page) =>  {
  await page.click('.main-nav .nav-li a')
  await LOG.success('起点中文网全部作品打开成功')
  await page.waitFor(2000)
  await LOG.success('全部作品页面打开成功，准备采集小说分类...')
  const menu = await page.$$eval('.select-list .type-filter ul li a', els => Array.prototype.slice.call(els).filter(el => {
    return el.innerText !== '全部'
  }).map(el => {
    return {
      menu: el.innerText,
      url:el.href
    }
  }))
  await LOG.success('一级菜单采集成功，准备写入数据库')
  const writeMenu = await menu.map(item => {
    return {
      menu: item.menu,
      level: 1,
      parentCode: '-1'
    }
  })
  const menuDB = await BookMenu.insertMany(writeMenu)
  await LOG.success('一级菜单入库成功')
  const mergeMenu = menu.map((item, index) => {
    return Object.assign(item, {
      _id: menuDB[index]._id
    })
  })
  for (let i = 0; i < mergeMenu.length; i++) {
    await submenu(page, mergeMenu[i])
  }
  await LOG.success('全部菜单写入成功')
}

/*
 *@Description: 采集二级菜单
 *@params: 
 *@Author:  lupan
 *@Date: 2019-06-19 11:16:10
*/
const submenu = (page, menu) => {
  return new Promise(resolve => {
    (async() => {
      await page.goto(menu.url)
      await LOG.success(`${menu.menu}菜单页面打开成功，准备采集二级菜单`)
      const submenu = await page.$$eval('.select-list .type-filter .sub-type dl:not(.hidden) a', els => Array.prototype.slice.call(els).map(el => el.innerText))
      const submenuDB = submenu.map(item => {
        return {
          menu: item,
          parentCode: menu._id,
          level: 2
        }
      })
      await BookMenu.insertMany(submenuDB)
      await LOG.success(`${menu.menu}二级菜单写入成功...`)
      resolve()
    })()
  }) 
}

// 起点中文网全部作品
const url = 'https://www.qidian.com/'
const qidian = {
  gat: async (page) => {
    await LOG.success('起点中文网即将采集...');
    await page.goto(url)

    // 检测小说菜单是否采集
    let menuData = await BookMenu.find()
    if (!menuData.length) {
      await LOG.info('小说菜单未采集，准备采集')
      await gatMenu(page)
    }
    menuData = await BookMenu.find({'level': 2}, {'__v': 0, 'level': 0, 'parentCode': 0})
    // 将小说菜单存入缓存中
    global.menuData = menuData

    // 开始采集小说数据
    await page.click('.main-nav .nav-li a')
    await LOG.success('起点中文网全部作品打开成功')
    await page.waitFor(2000)
    // const bookData = await page.$$eval('.all-book-list .book-img-text .all-img-list li', )
  }
}

export default qidian